<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>出库登记复核</title>
    <link rel="stylesheet" href="../css/productionPlanRegistrationCss.css">

    <link rel="stylesheet" href="../static/css/style.css">

    <script src="../js/jquery-2.1.1.min.js"></script>
    <!-- 新 Bootstrap4 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
    <link rel="stylesheet" href="../css/bootstrap-table.min.css">
    <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>

    <!-- 最新的 Bootstrap4 核心 JavaScript 文件 -->
    <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
    <script src="../static/js/global.min.js"></script>
    <script src="../js/bootstrap-table.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/bootstrap-table/1.21.0/locale/bootstrap-table-zh-CN.js"></script>
    <script>
        //渲染表格
        $(function () {
            $('#table').bootstrapTable({
                //请求方式  get请求中文会乱码，建议使用post请求
                method: 'post',
                //请求头部的设置
                contentType: 'application/json',
                //路径
                url: '/hj/queryWarehouseRegistrationCheck.action',
                //设置交替行样式
                striped: true,
                //初始显示页号
                pageNumber: 1,
                //设置表格高度
                height: 600,
                //是否显示分页
                pagination: true,
                //设置是服务端分页还是客户端分页
                sidePagination: 'server',
                //页大小
                pageSize:4,
                //分页列表
                pageList:[4,8],
                //上传到服务端的参数
                queryParams: function (params) {
                    return {
                        // 计算页号(当前页)
                        pageNum: (params.offset / params.limit) + 1,
                        // 计算页大小
                        pageSize: params.limit,

                    };
                },
                // 定义列名
                columns: [
                    {title: '出库单编号', field: 'payId', align: 'center'},
                    {title: '出库理由', field: 'reason', align: 'center'},
                    {title: '出库详细理由', field: 'reasonexact', align: 'center'},
                    {title: '登记人', field: 'register', align: 'center'},
                    {title: '登记时间', field: 'registerTime', align: 'center'},
                    {title: '总件数', field: 'amountSum', align: 'center'},
                    {title: '总金额（元）', field: 'costPriceSum', align: 'center'},
                    {title: '复核', field: 'checkTag', align: 'center',
                        formatter: function (value,row,index) {
                            return "<button data-toggle=\"modal\" class='btn btn-info' onclick='make("+JSON.stringify(row)+")'>登记</button>";
                        },
                    }
                ]
            });
        })
        var oj="";//库存
        var list;
        function make(obj){
            oj = obj;
            $('#payId').val(obj.payId);//出库单编号
            $('#reason').val(obj.reason);//出库理由
            $('#amountSum').val(obj.amountSum);//应出库总件数
            $('#paidAmountSum').val(obj.paidAmountSum);//已出库总件数
            $('#costPriceSum').val(obj.costPriceSum);//应出库总金额
            $('#register').val(obj.register);//登记人
            $('#registerTime').val(obj.registerTime);//登记时间
            $.ajax({
                type:'POST',
                url:'/hj/queryOutboundDeliveryOrder.action',
                data:'id='+obj.id,
                success:function (SPayDetailsView){
                    $('#tbodyModal').html("");
                    $('#outAmountTotal').val(SPayDetailsView.outMoneyTotal.outAmountTotal);//已出库总件数
                    $('#outMoneyTotal').val(SPayDetailsView.outMoneyTotal.outMoneyTotal);//已出库总金额
                    list = SPayDetailsView.sPayDetailsVos;
                    $.each(SPayDetailsView.sPayDetailsVos,function (index,SPayDetail){
                        $('#tbodyModal').append("<tr class='tr'>" +
                            "<td>"+SPayDetail.product_id+"</td>" +
                            "<td>"+SPayDetail.product_name+"</td>" +
                            "<td>成品库</td>" +
                            "<td>"+SPayDetail.cost_price+"</td>" +
                            "<td>"+SPayDetail.number+"</td>" +
                            "<td>"+SPayDetail.amount+"</td>" +
                            "<td>"+SPayDetail.paid_amount+"</td>" +
                            "<td class='td'>"+(SPayDetail.amount==SPayDetail.paid_amount?'已完成':'<input type="text" value='+SPayDetail.outNumber+' class="form-control" style="text-align: center">')+"</td>" +
                            "</tr>")
                    });
                    $('#myModal').modal("show");
                }
            })
        }
        $(function (){
            $('#submit').click(function (){
                let f = true;
                let total = 0;
                let text = "";
                $('.td').each(function (i,e){
                    if($(e).children().val()!=""){
                        let number = parseInt($(e).children().val())+parseInt($(e).prev().text());
                        if(number>$(e).prev().prev().text()){
                            f = false;
                            text="出库数量大于当前可存放数量或应出库数量,请重新输入！！！";
                        }else{
                            if($(e).text()!="已完成"){
                                total =total+parseInt($(e).children().val());
                            }
                        }
                    }else{
                        text = "出库数量不能为空！！！";
                        f = false;
                    }

                });
                if(f===true){
                    let array1 = new Array();//修改库存明细
                    let array2 = new Array();//新增库存明细
                    let date = new Date();//定义时间
                    $.each(list,function (i,v){
                        $.each($('.tr'),function (j,g){
                            if(v.product_id==$(g).children().eq(0).text() && v.amount>0 && $(g).children().eq(7).text()!="已完成" && $(g).children().eq(7).children().val()>0){
                                let sPayDetails1 = {
                                    id:v.id,
                                    productId:v.product_id,
                                    paidAmount:$(g).children().eq(7).children().val(),
                                }
                                let sPayDetails2 = {
                                    parentId:v.id,//父级序号
                                    subtotal:$(g).children().eq(3).text()*$(g).children().eq(7).children().val(),//小计
                                    paidAmount:$(g).children().eq(7).children().val(),//确认出库件数
                                    dateTime:date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate()+" "+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds(),//登记时间
                                }
                                array1.push(sPayDetails1);
                                array2.push(sPayDetails2);
                            }
                        })
                    })

                    let SPayView = {
                        sPay:{
                            id:oj.id,
                            paidAmountSum:total,
                        },
                        sPayDetailsList:array1,
                        savesPayDetailsList:array2,
                    }
                    //提交出库单
                    $.ajax({
                        type:'POST',
                        contentType: 'application/json;charset=utf-8',
                        url:'/hj/updateWarehouseRegistrationCheck.action',
                        data: JSON.stringify(SPayView),
                        success:function (resultData){
                            if(resultData>0){
                                $('#myModal').modal("hide");
                                alert("成功操作一条数据");
                                $('#table').bootstrapTable("refresh");
                            }else {
                                alert("操作一条数据失败");
                            }
                        }
                    })
                }else{
                    alert(text);
                }
            });
        })
    </script>
</head>
<body>

    <table id="table"></table>

    <div class="modal fade" id="myModal">
        <div class="modal-dialog modal-xl">
            <div class="modal-content">

                <!-- 模态框头部 -->
                <div class="modal-header">
                    <h4 class="card-title">reviewProductionPlan</h4>
                    <div>
                        <button type="button" class="btn btn-info" data-dismiss="modal" id="submit">提交</button>
                        <button type="button" class="btn btn-dark" data-dismiss="modal">返回</button>
                    </div>
                </div>

                <!-- 模态框主体 -->
                <div class="modal-body">
                    <div class="d1">
                        <div style="margin-top: 10px"><h2>出库单</h2>

                        </div>
                        <div class="container-fluid row" style="margin-top: 10px">
                            <div class="input-group mb-4 col">
                                <div class="input-group-prepend">
                                    <span class="input-group-text">出库单编号<span class="text-danger scale5 ms-2">*</span></span>
                                </div>
                                <input type="text" class="form-control" id="payId" style="background-color: white" readonly>
                            </div>

                            <div class="input-group mb-4 col">
                                <div class="input-group-prepend">
                                    <span class="input-group-text">出库理由<span class="text-danger scale5 ms-2">*</span></span>
                                </div>
                                <input type="text" class="form-control" id="reason" style="background-color: white" readonly>
                            </div>
                        </div>

                        <div class="container-fluid row">
                            <table class="table table-bordered" style="text-align: center">
                                <thead>
                                <tr>
                                    <th>产品编号</th>
                                    <th>产品名称</th>
                                    <th>库房名称</th>
                                    <th>单价($)</th>
                                    <th>当前库存数量</th>
                                    <th>应出库件数</th>
                                    <th>已出库件数</th>
                                    <th>本次出库数量</th>
                                </tr>
                                </thead>
                                <tbody id="tbodyModal">


                                </tbody>
                            </table>
                        </div>

                        <div class="container-fluid row">
                            <div class="input-group mb-4 col">
                                <div class="input-group-prepend">
                                    <span class="input-group-text">应出库总件数<span class="text-danger scale5 ms-2">*</span></span>
                                </div>
                                <input type="text" class="form-control" id="amountSum" style="background-color: white" readonly>
                            </div>
                            <div class="input-group mb-4 col">
                                <div class="input-group-prepend">
                                    <span class="input-group-text">已出库总件数<span class="text-danger scale5 ms-2">*</span></span>
                                </div>
                                <input type="text" class="form-control" id="outAmountTotal" style="background-color: white" readonly>
                            </div>
                        </div>

                        <div class="container-fluid row">
                            <div class="input-group mb-4 col">
                                <div class="input-group-prepend">
                                    <span class="input-group-text">应出库总成本<span class="text-danger scale5 ms-2">*</span></span>
                                </div>
                                <input type="text" class="form-control" id="costPriceSum" style="background-color: white" readonly>
                            </div>
                            <div class="input-group mb-4 col">
                                <div class="input-group-prepend">
                                    <span class="input-group-text">已出库总成本<span class="text-danger scale5 ms-2">*</span></span>
                                </div>
                                <input type="text" class="form-control" id="outMoneyTotal" style="background-color: white" readonly>
                            </div>
                        </div>

                        <div class="container-fluid row">
                            <div class="input-group mb-4 col">
                                <div class="input-group-prepend">
                                    <span class="input-group-text">登记人<span class="text-danger scale5 ms-2">*</span></span>
                                </div>
                                <input type="text" class="form-control" id="register" style="background-color: white" readonly>
                            </div>
                            <div class="input-group mb-4 col">
                                <div class="input-group-prepend">
                                    <span class="input-group-text">登记时间<span class="text-danger scale5 ms-2">*</span></span>
                                </div>
                                <input type="text" class="form-control" id="registerTime" style="background-color: white" readonly>
                            </div>
                        </div>

                        <div class="col" style="margin-bottom: 10px">
                            <span><h6>出库详细理由:</h6></span>
                            <textarea class="form-control" rows="4" id="remark"
                                      style="background-color: white;height: 115px;border-color: #c7c7c7;resize:none" readonly></textarea>
                        </div>

                    </div>

                </div>

                <!-- 模态框底部 -->
                <div class="modal-footer">
                    <button type="button" class="btn btn-danger" data-dismiss="modal">关闭</button>
                </div>

            </div>
        </div>
    </div>

</body>
</html>